﻿package com.hnev.gui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import com.hnev.SystemData.SQL;


public class AdminSystem extends JFrame{
	private JButton buttonDel,buttonAdd,buttonAlter,buttonSX;
	private JTable table;
	private JScrollPane jScrollPane;
	private DefaultTableModel model;
	private JLabel labID,labName,labPrice,labUnic;
	private JPanel panel1,panel2,panel3;
	private JTextField tfID, tfName, tfPrice, tfUnic;
	
	public AdminSystem(){
		model = new DefaultTableModel();
		initData();
		
		table = new JTable(model);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jScrollPane = new JScrollPane(table);
		
		panel1 = new JPanel();
		panel2 = new JPanel();
		panel3 = new JPanel();
		
		this.setLayout(null);
		panel1.setLayout(new GridLayout(1,4));
		panel2.setLayout(null);
		panel3.setLayout(null);
		
		labID = new JLabel("编号",SwingConstants.CENTER);
		labName = new JLabel("名称",SwingConstants.CENTER);
		labPrice = new JLabel("单价",SwingConstants.CENTER);
		labUnic = new JLabel("单位",SwingConstants.CENTER);
		
		panel1.setBounds(0, 0, 470, 40);
		
		panel1.add(labID);
		panel1.add(labName);
		panel1.add(labPrice);
		panel1.add(labUnic);
		
		
		panel2.setBounds(0,30, 470, 200);
		jScrollPane.setBounds(0, 10, 470, 210);
		
		panel2.add(jScrollPane);
		
		buttonAdd = new JButton("新增");
		buttonDel = new JButton("删除");
		buttonAlter = new JButton("修改");
		buttonSX = new JButton("刷新");
		
		

		tfID = new JTextField(5);
		tfID.setBounds(10, 10, 90, 30);
		tfName = new JTextField(10);
		tfName.setBounds(120, 10, 90, 30);
		tfPrice = new JTextField(5);
		tfPrice.setBounds(230, 10, 90, 30);
		tfUnic = new JTextField(10);
		tfUnic.setBounds(340, 10, 90, 30);
		panel3.setBounds(0, 230, 470, 240);
		buttonDel.setBounds(0, 60, 80, 40);
		buttonAdd.setBounds(125, 60, 80, 40);
		buttonAlter.setBounds(250, 60, 80, 40);
		buttonSX.setBounds(375, 60, 80, 40);
		panel3.add(tfID);
		panel3.add(tfName);
		panel3.add(tfPrice);
		panel3.add(tfUnic);
		panel3.add(buttonAlter);
		panel3.add(buttonAdd);
		panel3.add(buttonDel);
		panel3.add(buttonSX);
		
		//设置按键功能
		buttonAlter.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String id, name, price, unic;
				id = tfID.getText();
				name = tfName.getText();
				price = tfPrice.getText(); 
				unic = tfUnic.getText();

				String sqlWord = "UPDATE fruits_info SET NAME=?, price =?, unic=? WHERE id=?";
				String[] params = new String[] { name, price, unic ,id };

				SQL sql = new SQL();
				sql.getConnection();

				int result = sql.executeUpdate(sqlWord, params);

				sql.classAll();

				if (result > 0) {
					// 修改成功
					JOptionPane
							.showMessageDialog(AdminSystem.this, "修改成功");
				} else {
					// 修改失败
					JOptionPane
							.showMessageDialog(AdminSystem.this, "修改失败");
				}
				initData();
			}
		});
		buttonAdd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				

				String name,unic,price,id;
				
				id = tfID.getText();
				name = tfName.getText();
				price = tfPrice.getText(); 
				unic = tfUnic.getText();
				
				String sqlWord = "INSERT  INTO fruits_info VALUES (?,?,?,?);";
				String[] params = new String[] {id,name,price,unic};
				
				SQL sql = new SQL();
				sql.getConnection();
				int result = sql.executeUpdate(sqlWord, params);
				sql.classAll();
				if (result > 0) {
					// 添加成功
					JOptionPane
							.showMessageDialog(AdminSystem.this, "添加成功");
				} else {
					// 添加失败
					JOptionPane
							.showMessageDialog(AdminSystem.this, "添加失败");
				}
				initData();
			
			}
		});
		buttonDel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int[] indexs = table.getSelectedRows();
				if (indexs.length == 0) {

				}
				if (indexs.length > 1) {

				}
				if (indexs.length == 1) {
					// P202 -10

					int index = indexs[0];
					SQL sql = new SQL();
					sql.getConnection();
					
					String id = table.getValueAt(index, 0).toString();
					String sqlword = "delete from fruits_info where id=" + id;

					int result = sql.executeUpdate(sqlword, null);

					sql.classAll();
					if (result > 0) {
						// 删除成功
						JOptionPane.showMessageDialog(AdminSystem.this,
								"删除成功");
					} else {
						// 删除失败
						JOptionPane.showMessageDialog(AdminSystem.this,
								"删除失败");
					}
					initData();

				}
			}
		});
		buttonSX.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				initData();
			}
		});
		
		this.add(panel1);
		this.add(panel2);
		this.add(panel3);
		setLocation(520, 300);
		setSize(470, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		
		
	}

	private void initData() {
		SQL sql = new SQL();
		try {
			sql.getConnection();
			String sqlword = "select * from fruits_info order by id";
			ResultSet rs = sql.executeQuery(sqlword, null);
			ResultSetMetaData rsmd = rs.getMetaData();
			
			int colCount = rsmd.getColumnCount();
			Vector<String> title = new Vector<String>();
			for(int i = 1; i <= colCount; i++) {
				title.add(rsmd.getColumnLabel(i));
			}
			
			int rowCount = 0;
			Vector<Vector<String>> data = new Vector<Vector<String>>();
			while(rs.next()){
				rowCount++;
				Vector<String> rowData = new Vector<String>();
				for(int i = 1; i <= colCount; i++){
					rowData.add(rs.getString(i));
				}
				data.add(rowData);
			}
			
			if(rowCount > 0){
				model.setDataVector(data, title);
			}else{
				model.setDataVector(null, title);
			}
			
			
			System.out.println(title);
		} catch (Exception e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		sql.classAll();
	}
	public static void main(String[] args) {
		new AdminSystem();
	}
}
